package com.ruoyi.order.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.basic.service.impl.BaseServiceImpl;
import com.param.concat.IManuscriptParams;
import com.param.concat.IToExamineParamBySearch;
import com.ruoyi.order.domain.Manuscript;
import com.ruoyi.order.mapper.ManuscriptMapper;
import com.ruoyi.order.service.IManuscriptService;
import com.ruoyi.oss.config.OssConfig;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 稿件Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-08
 */
@Service
public class ManuscriptServiceImpl extends BaseServiceImpl<Long, Manuscript> implements IManuscriptService 
{
	
	private OssConfig ossConfig;
	
	private static int[] SERVICE_TYPE = new int[]{
			
			// 1 反馈 2 售后
		-1,	
		11,
		10, // 10 二次服务-售后返稿  11 二次服务-反馈返稿
	};
	

	@Resource
	public void setOssConfig(OssConfig ossConfig) {
		this.ossConfig = ossConfig;
	}
	
    public ManuscriptServiceImpl(ManuscriptMapper baseMapper) {
		super(baseMapper);
	}

    
    
    @Override
	public List<Manuscript> selectListById(int id) {
		// TODO Auto-generated method stub
		List<Manuscript> selectListById = super.selectListById(id);
		
		String url = "http://" + ossConfig.getURL();
		
		for (Manuscript manuscript : selectListById) {
            manuscript.setFile(url + manuscript.getFile());
        }
		
		return selectListById;
	}

	/**
     * 查询稿件
     * @param id 稿件主键
     * @return 稿件
     */
    @Override
    public Manuscript selectById(Integer id)
    {
        return baseMapper.selectById(id);
    }



	@Override
	protected Class<Manuscript> getEntityClass() {
		// TODO Auto-generated method stub
		return Manuscript.class;
	}

	@Override
	protected Manuscript[] getEntityObject(int size) {
		// TODO Auto-generated method stub
		return new Manuscript[size];
	}

	/**
     * 查询稿件列表
     * 
     * @param manuscript 稿件
     * @return 稿件
     */
    @Override
    public List<Manuscript> selectList(Manuscript manuscript)
    {
        return baseMapper.selectList(manuscript);
    }

    /**
     * 新增稿件
     * 
     * @param manuscript 稿件
     * @return 结果
     */
    @Override
    public int insert(Manuscript manuscript)
    {
        return baseMapper.insert(manuscript);
    }

    /**
     * 修改稿件
     * 
     * @param manuscript 稿件
     * @return 结果
     */
    @Override
    public int update(Manuscript manuscript)
    {
        return baseMapper.update(manuscript);
    }

    /**
     * 批量删除稿件
     * 
     * @param ids 需要删除的稿件主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids)
    {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 删除稿件信息
     * 
     * @param id 稿件主键
     * @return 结果
     */
    @Override
    public int deleteById(Integer id)
    {
        return baseMapper.deleteById(id);
    }

	@Override
	public List<Manuscript> getManuscriptByOrderIds(int[] ids) {
		
		ManuscriptMapper manuscriptMapper =  this.getBaseMapper();
		
		List<Manuscript> manuscriptByOrderIds = manuscriptMapper.getManuscriptByOrderIds(ids);
		
		if (0 == manuscriptByOrderIds.size()) {
			
			return manuscriptByOrderIds;
		}
		
		String url = "http://" + ossConfig.getURL();
		
		for (Manuscript manuscript : manuscriptByOrderIds) {
			
			manuscript.setFile(url.concat(manuscript.getFile()));
		}
		
		return manuscriptByOrderIds;
		
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public <M extends IManuscriptParams> int toExamination(IToExamineParamBySearch<M> param) {
		
		ManuscriptMapper manuscriptMapper =  this.getBaseMapper();
		
		M[] files = param.getFiles();
		
		if (null == files || 0 == files.length) {
			return 0;
		}
		
		Manuscript[] manuscripts = new Manuscript[files.length];
		
		int orderLogId = param.getOrderLogId();
		
		Byte serviceType = param.getServiceType();
		
		Manuscript manuscript ;
		
		int id = param.getId();
		
		String url = ossConfig.getURL();
		
		Integer payManagerId = param.getPayManagerId();
		
		long timeMillis = System.currentTimeMillis() / 1000;
		
		int userId = param.getUserId();
		
		for (int i = 0; i < files.length; i++) {
			manuscript = new Manuscript();
			
            String file = files[i].getFile();
            
            file = file.replace("http://","").replace("https://", "").replace(url, "");
            
			manuscript.setFile(file);
            
            manuscript.setOrderId(id);
            
            manuscript.setPayManagerId(payManagerId);
            
            manuscript.setUserId(userId);
            
            manuscript.setCreateTime(timeMillis);
            
            manuscript.setUpdateTime(timeMillis);
            
            manuscript.setStatus(SERVICE_TYPE[serviceType]);
            
            manuscript.setOrderLogId(orderLogId);
            
            manuscript.setOriginName(files[i].getOriginName());;
            
            manuscript.setSize(files[i].getSize().intValue());;
            
            manuscripts[i] = manuscript;
        }
		
		return manuscriptMapper.batchInsert(manuscripts);
	}
}
